﻿using System.Data;
using MySqlConnector;

namespace Front._1125;

public partial class BookFrom : Form
{
    public BookFrom()
    {
        InitializeComponent();
    }

    private void 图书名称_Click(object sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }

    private void BookFrom_Load(object sender, EventArgs e)
    {
        GetAllBooks();
        GetAllCategories();
    }

    private void GetAllBooks()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=root;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        String sql = "SELECT book_id,isbn,book_name FROM book_info";
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        DataSet dataSet = new DataSet();
        mySqlDataAdapter.Fill(dataSet,"book_info");
        this.dataGridView1.DataSource = dataSet.Tables["book_info"];
    }

    private void GetAllCategories()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=root;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        String sql = "SELECT distinct book_category FROM book_info";
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        DataSet dataSet = new DataSet();
        mySqlDataAdapter.Fill(dataSet,"book_category");
        if (dataSet.Tables["book_category"].Rows.Count > 0)
        {
            DataTable categoryTable= dataSet.Tables["publisher_info"];
            // 插入一行全部
            DataRow row = categoryTable.NewRow();
            row[0] = "全部";
    
            categoryTable.Rows.InsertAt(row,0);
            comboBox1.DataSource = categoryTable;
            comboBox1.DisplayMember = categoryTable.Columns[0].ColumnName;
            
        }
    }
}